Skip to content

feat: Refactor LLM model zoo and add KV cache support #3527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open

Conversation

peri044
Copy link
Collaborator

@peri044 peri044 commented May 20, 2025

Description

This PR redesigns our LLM model compilation, unifies it, fixes output mismatch and performance issues. This PR also implements KV caching using native TensorRT.

Fixes # (issue)

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@peri044 peri044 added the WIP Work is in progress, pull request should not be merged yet label May 20, 2025
@peri044 peri044 marked this pull request as draft May 20, 2025 23:30
@github-actions github-actions bot added component: lowering Issues re: The lowering / preprocessing passes component: conversion Issues re: Conversion stage component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels May 20, 2025
@github-actions github-actions bot requested a review from gs-olive May 20, 2025 23:30
@peri044 peri044 changed the title feat : caching attempts feat: caching attempts May 20, 2025
@peri044 peri044 removed the request for review from gs-olive May 21, 2025 00:40
github-actions[bot]

This comment was marked as spam.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as spam.

github-actions[bot]

This comment was marked as spam.

@peri044 peri044 removed WIP Work is in progress, pull request should not be merged yet labels Jun 12, 2025
@peri044 peri044 marked this pull request as ready for review June 12, 2025 22:36
github-actions[bot]

This comment was marked as spam.

github-actions[bot]

This comment was marked as spam.

@peri044 peri044 changed the title feat: caching attempts feat: Refactor LLM optimization and add KV cache support Jun 13, 2025
@peri044 peri044 changed the title feat: Refactor LLM optimization and add KV cache support feat: Refactor LLM model zoo and add KV cache support Jun 13, 2025
@github-actions github-actions bot removed the component: lowering Issues re: The lowering / preprocessing passes label Jun 13, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jun 13, 2025
@@ -0,0 +1,193 @@
import torch
Copy link
Collaborator

@narendasan narendasan Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a tests/py/tools/llm?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can. These tests helped me debug accuracy/functionality issues during the development. It would take some time to get them working in CI. I can add them as a part of cherry-pick PR after this. What do you think ?

Copy link
Collaborator

@zewenli98 zewenli98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM. just some questions

@@ -837,6 +859,7 @@ def contains_metadata(gm: torch.fx.GraphModule) -> bool:
continue
submodule_node_dict[node.name] = node

preserve_module_specs(original_in_spec, original_out_spec, partitioned_module)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should set spec for partitioned_module's children?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec for children is actually set by the partitioning. I'm only trying to preserve the output spec for the entire partitioned_module (It should be the same as original module unless we change it explicitly which is the case in KV caching)

from utils import export_llm


def export_llm(model, inputs, min_seq_len=1, max_seq_len=16):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not directly call tools/llm/utils.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to use sys.append to include the path to tools/llm/utils.py to do that. I wanted this example to be standalone so I just copied this function.

Copy link
Collaborator

@zewenli98 zewenli98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same questions above

Copy link
Collaborator

@narendasan narendasan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM only suggestion is moving tests into a tests subdirectory or into the tests directory. Is optional.

return timings


def recordStats(backend, timings, precision, batch_size=1, compile_time_s=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor but everything else is snake case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: runtime documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants